package top.korori.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.korori.common.model.Result;
import top.korori.system.annotation.OptLog;
import top.korori.system.annotation.PreAuth;
import top.korori.system.constant.SysOptTypeConst;
import top.korori.system.entity.SysMenu;
import top.korori.system.entity.bo.SysMenuBo;
import top.korori.system.service.SysMenuService;
import top.korori.system.entity.vo.SysMenuVo;

import javax.validation.Valid;
import java.util.List;


/**
 * 
 * 后台菜单表 前端控制器
 * 
 *
 * @author korori
 */
@Api(tags = "系统菜单")
@RestController
@RequestMapping("/sys/menu")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    @ApiOperation("获取树状数据")
    @PreAuth("sys:menu:list")
    @GetMapping("/tree")
    public Result<List<SysMenuVo>> tree(){
        List<SysMenuVo> sysMenuVos = sysMenuService.tree();
        return Result.ok(sysMenuVos);
    }

    @ApiOperation("获取选择用菜单树")
    @GetMapping("/selectTree")
    public Result<List<SysMenuVo>> selectTree(){
        List<SysMenuVo> sysMenuVos = sysMenuService.selectTree();
        return Result.ok(sysMenuVos);
    }

    @ApiOperation("添加菜单")
    @OptLog(optType = SysOptTypeConst.ADD)
    @PreAuth("sys:menu:add")
    @PostMapping("/add")
    public Result<Void> add(@RequestBody @Valid SysMenuBo sysMenuBo){
        sysMenuService.add(sysMenuBo);
        return Result.ok();
    }

    @ApiOperation("获取菜单")
    @PreAuth("sys:menu:query")
    @GetMapping("/get/{menuId}")
    public Result<SysMenu> get(@PathVariable Long menuId){
        SysMenu sysMenu = sysMenuService.getById(menuId);
        return Result.ok(sysMenu);
    }

    @ApiOperation("修改菜单")
    @OptLog(optType = SysOptTypeConst.UPLOAD)
    @PreAuth("sys:menu:edit")
    @PutMapping("/edit")
    public Result<Void> edit(@RequestBody @Valid SysMenuBo sysMenuBo){
        sysMenuService.edit(sysMenuBo);
        return Result.ok();
    }

    @ApiOperation("删除菜单")
    @OptLog(optType = SysOptTypeConst.DEL)
    @PreAuth("sys:menu:del")
    @DeleteMapping("/del/{menuIds}")
    public Result<Void> del(@PathVariable List<Long> menuIds){
        sysMenuService.del(menuIds);
        return Result.ok();
    }

}



















